define(function (require, exports, module) {
//    require("echart/echart");
//    require("echarts/chart/pie");
    var ec = require("echarts/echarts");
    var themes = require("echarts/theme/gray");
    require("zrender/zrender");

    exports.init = function (ctx, uuid) {
        var page = new $.PageDomFactory(uuid);

        page.getJqDom("searchType").combobox({
            panelHeight: 'auto',
            editable: false
        }); //初始化下拉框

        function initReportPanel() {
            page.getJqDom("panel_left_report").panel({
                height: 450
            });
            //init right
            page.getJqDom("panel_right_report").panel({
                height: 450,
                fitColumn: true
            });
        }

        /*
         * 饼状图
         */
        function piereport() {
            var postData = page.getJqDom("form-pie").serializeJson();
            $.post(ctx + "/app/api/sysLog/report4Pie", {
                "beginDate": postData.beginDate,
                "endDate": postData.endDate
            }, function (jsonStr) {
                if (jsonStr != '') {
                    require("echarts/chart/pie");
                    var myChart = ec.init(page.getJqDom("panel_right_report")[0], themes);
                    // 为echarts对象加载数据
                    var PieChartOption = getPieChartOption(jsonStr, postData);
                    myChart.setOption(PieChartOption);
                }
            }, "JSON");
        }

        /*
         * 折线报表
         */
        function linereport() {
            var postData = page.getJqDom("form-line").serializeJson();
            $.post(ctx + "/app/api/sysLog/report4Line", {
                "beginDate": postData.beginDate,
                "endDate": postData.endDate,
                'searchType': postData.searchType
            }, function (jsonStr) {
                if (jsonStr != '') {
                    require("echarts/chart/line");
                    // 基于准备好的dom，初始化echarts图表
                    var myChart = ec.init(page.getJqDom("panel_left_report")[0], themes);
                    // 为echarts对象加载数据
                    myChart.setOption(getLineOption(jsonStr, postData));

                } else {
                    page.getJqDom("hide").show();
                }
            }, "JSON");
        }

        initReportPanel();

        page.getJqDom("lineReport").linkbutton({
            onClick: function () {
                linereport();
            }
        });
        page.getJqDom("piereport").linkbutton({
            onClick: function () {
                piereport();
            }
        });
        piereport();
        linereport();
        /*
         * 拼装饼状图数据
         */
        function getPieChartOption(jsonStr, postData) {
            var PieChartOtion = {
                title: {
                    text: '系统日志分析图',
                    x: 'center'
                },
                tooltip: {
                    trigger: 'item',
                    formatter: jsonStr.legend.length > 0 ? "{a} <br/>{b} : {c}({d}%)" : "无日志"
                },
                legend: {
                    orient: 'horizontal',
                    y: 'bottom',
                    data: jsonStr.legend
                },
                toolbox: {
                    show: true,
                    feature: {
                        restore: {show: true, title: '刷新'},
                        saveAsImage: {
                            show: true,
                            name: postData.beginDate + "-" + postData.endDate + "日志饼图"
                        }
                    }
                },
                calculable: false,
                series: [
                    {
                        name: '日志数量(个)',
                        type: 'pie',
                        radius: '55%',
                        clockWise: false,
                        selectedMode: 'single',
                        center: ['50%', '50%'],
                        itemStyle: {
                            normal: {
                                label: {
                                    show: true,
                                    textStyle: {
                                        color: '#333333'
                                    }
                                }
                            }
                        },
                        data: jsonStr.series
                    }
                ]
            };
            return PieChartOtion;

        }

        /*
         *拼装折线图数据
         */
        function getLineOption(jsonStr, postData) {
            var option = {
                title: {
                    text: '系统日志数监控',
                    x: 'center'
                },
                tooltip: {
                    trigger: 'axis'
                },
                legend: {
                    orient: 'horizontal',
                    y: 'bottom',
                    data: jsonStr.legend
                },
                toolbox: {
                    show: true,
                    feature: {
                        restore: {
                            show: true,
                            title: '刷新'
                        },
                        saveAsImage: {
                            show: true,
                            name: postData.beginDate + "-" + postData.endDate + "日志折线图"
                        }
                    }
                },
                calculable: false,
                xAxis: [
                    {
                        type: 'category',
                        boundaryGap: false,
                        data: jsonStr.xAxis
                    }
                ],
                yAxis: [
                    {
                        type: 'value'
                    }
                ],
                series: [
                    {
                        name: 'INFO',
                        type: 'line',
                        data: jsonStr.series_info
                    },
                    {
                        name: 'WARN',
                        type: 'line',
                        data: jsonStr.series_warn
                    },
                    {
                        name: 'TRACE',
                        type: 'line',
                        data: jsonStr.series_trace
                    },
                    {
                        name: 'DEBUG',
                        type: 'line',
                        data: jsonStr.series_debug
                    } ,
                    {
                        name: 'ERROR',
                        type: 'line',
                        data: jsonStr.series_error
                    }
                ]
            };
            return option;
        }
    };
});